//
//  LKMyUpdateCustomerCheckViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/11.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKMyUpdateCustomerCheckViewController.h"
#import "LKWriteContinuInvestCell.h"
#import "LKMyDescriptionCell.h"
#import "LKTestpageNameTitleCell.h"
#import "LKOrderEditBottomView.h"
#import "LKMyUpdateCustomerCheckResultViewController.h"
@interface LKMyUpdateCustomerCheckViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSArray *titlesArray;
@property(nonatomic,strong)LKOrderEditBottomView *botomeView;
@property(nonatomic,copy)NSString *suggestStr;
@end

@implementation LKMyUpdateCustomerCheckViewController
-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _botomeView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditBottomView" owner:self options:nil] lastObject];
        [_botomeView.forwardBtn setTitle:@"驳回" forState:UIControlStateNormal];
        [_botomeView.forwardBtn addTarget:self action:@selector(resultVc:) forControlEvents:UIControlEventTouchUpInside];
        [_botomeView.nextBtn setTitle:@"同意" forState:UIControlStateNormal];
        [_botomeView.nextBtn addTarget:self action:@selector(resultVc:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_botomeView];
        [_botomeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(70);
        }];
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 12 + NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 12 + 70)) style:UITableViewStyleGrouped registercellClasss:@[@"LKTestpageNameTitleCell",@"LKWriteContinuInvestCell",@"LKMyDescriptionCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(void)resultVc:(UIButton *)btn{
    NSString *title = btn.titleLabel.text;
    
    NSString *approveStatuStr = [title isEqual:@"同意"] ? @"3":@"-3";
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:self.approvModel.applyId,@"Id",self.approvModel.customerId,@"CustomerId",approveStatuStr,@"ApprovalStatus",self.suggestStr,@"ApprovalContent",self.approvModel.customerCode,@"CustomerCode", nil];
    NSString *Str = [NSString convertToJsonData:param];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:commitCheckCustomerOwnerUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD showInfoWithStatus:@"审批成功"];
        [self.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RealoadChangeSuccessNotice" object:nil];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 200;
    [self getMyWaitDetailInforamtion];
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"客户归属变更审批";
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 || indexPath.section == 1 || indexPath.section == 2) {
        LKWriteContinuInvestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWriteContinuInvestCell"];
        cell.arrowIcon.hidden = YES;
        cell.titleLabel.text = self.titlesArray[indexPath.section];
        NSString *beforeContext = self.approvModel.beforeContext == nil ? @"":self.approvModel.beforeContext;
        NSString *afterContext = self.approvModel.afterContext == nil ? @"":self.approvModel.afterContext;
        NSArray *arra = @[self.approvModel.customerName,beforeContext,afterContext];
        cell.contentLabel.text = arra[indexPath.section];
        return cell;
    }
    if (indexPath.section == 3) {
        LKTestpageNameTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKTestpageNameTitleCell"];
        cell.titleLabel.text = @"备注";
        cell.contentLabel.text = self.approvModel.changeRemark;
        return cell;
    }
    LKMyDescriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyDescriptionCell"];
    cell.titleLabel.text = @"输入审批意见";
    WS(weakSelf)
    [[cell.textView rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        weakSelf.suggestStr = x;
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        return UITableViewAutomaticDimension;
    }
    return indexPath.section == 4 ? 226 : 43;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/**
 获取待审批客户归属变更的详细信息
 */
-(void)getMyWaitDetailInforamtion{
    
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:getMyWaitCheckCustomerOwnerChangeUrl header:[LKUserInforTool header] parameters:@{@"Id":self.approvModel.applyId} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"result"];
        self.approvModel = [LKApprovaModel modelWithDictionary:dic];
        self.approvModel.applyId = dic[@"applyId"];
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = @[@"选择客户",@"原客户经理",@"新客户经理"];
    }
    return _titlesArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
